package wowraidmanager.server.service.wishlist;

import java.util.ArrayList;

import org.slim3.datastore.Datastore;

import wowraidmanager.client.service.wishlist.UserStatusService;
import wowraidmanager.server.meta.wishlist.MemberMeta;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserStatusServiceImpl implements UserStatusService {

    MemberMeta query = MemberMeta.get();

    public UserStatus getUserStatus(){
        UserService userService = UserServiceFactory.getUserService();
        String currentUser = "";
        boolean isAdmin = false;
        if(userService.getCurrentUser() != null){
            currentUser = userService.getCurrentUser().getEmail();
            isAdmin = userService.isUserAdmin();
        }
        boolean isOfficer = false;
        boolean isUser = false;
        ArrayList<Member> currentMemberList = (ArrayList<Member>)Datastore.query(query).filter(query.memberAccount.equal(currentUser)).asList();

        Member currentMember = null;

        for(Member member:currentMemberList){
            if(member.getMembertype().equals("Officer") || member.getMembertype().equals("Member")){
                currentMember = member;
            }
        }

        if(currentMember != null){
            isOfficer = currentMember.getMembertype().equals(("Officer"));
            isUser = true;
        }else{
            isOfficer = false;
        }

        UserStatus userStatus = new UserStatus();
        userStatus.setAdmin(isAdmin);
        userStatus.setOfficer(isOfficer);
        userStatus.setUser(isUser);
        userStatus.setLogoutUrl(userService.createLogoutURL("/wishlist"));
        userStatus.setMember(currentMember);
        return userStatus;
    }
}
